import java.text.Collator;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

import javax.swing.AbstractListModel;


public class SortingListModel  extends AbstractListModel<String> {

	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// Define a SortedSet
    TreeSet<String> model;
    
    private static Comparator<String> USEFUL_COMPARATOR 
        = new Comparator<String>()
            {
                public int compare( String o1, String o2 )
                {
                    //String str1 = o1.toString();
                    //String str2 = o2.toString();
                    Collator collator = Collator.getInstance();
                    int result = collator.compare( o1, o1 );
                    return result;
                }
            };

    public SortingListModel()
    {
        model = new TreeSet<String>( USEFUL_COMPARATOR );
    }

	
	
	
	
	
	
	@Override
	public int getSize() {
		return model.size();
	}

	@Override
	public String getElementAt(int index) {
		// Return the appropriate element
        return (String)model.toArray()[index];
	}
	
	
	 // Other methods
    public void addElement( String element )
    {
        
        if( model.add( element ))
        {
            fireContentsChanged( this, 0, getSize() );
        }
    }

    public void addAll( String elements[] )
    {
        Collection<String> c = Arrays.asList(elements);
        model.addAll(c);
        fireContentsChanged( this, 0, getSize() );
    }

    public void clear()
    {
        model.clear();
        fireContentsChanged( this, 0, getSize() );
    }

    public boolean contains( String element )
    {
        return model.contains( element );
    }

    public String firstElement()
    {
        // Return the appropriate element
        return model.first();
    }

    public Iterator<String> iterator()
    {
        return model.iterator();
    }

    public String lastElement()
    {
        // Return the appropriate element
        return model.last();
    }

    public boolean removeElement( String element )
    {
        boolean removed = model.remove( element );
        if( removed )
        {
            fireContentsChanged( this, 0, getSize() );
        }
        return removed;
    }

    public String get(int index){
    	return (String) model.toArray()[index];
    }

}
